<?php
/**
 * Created by PhpStorm.
 * User: Qarluq
 * Date: 15/7/6
 * Time: 上午11:57
 */
class MemberAction extends CommonAction{
    //会员首页
    public function index(){

        import('ORG.Util.Page');
        $count=M('member')->count();
        $page=new page($count,10);
        $limit=$page->firstRow.','.$page->listRows;
        if(isset($_POST['mem_name'])){
            $name=$_POST['mem_name'];
            $where['mem_name']  = array('like', '%'.$name.'%');
            $db=M('member')->where($where)->limit($limit)->select();
        }else{
            $db=M('member')->limit($limit)->select();
        }
        $this->members=$db;
        $this->page=$page->show();
        $this->display();
    }
    //添加会员模板导入
    public function addmember(){
        $this->display();
    }
    //添加会员处理
    public function run_addmember(){

        $user = M('member')->where(array('mem_name' => array('eq', I('mem_name'))))->select();
        if ($user) {
            $this->error('账户已存在');
        }
        if (I('mem_password_1') != I('mem_password')) {
            $this->error('两次密码不配');
        }
        $data=array(
            "mem_name" => I('mem_name'),
            "mem_phone" => I('mem_phone'),
            "mem_email" => I('mem_email'),
            "mem_password" => I('mem_password','','md5'),
            "mem_number" => I('mem_number'),
            "lock" => I('lock'),
            "mem_status" => I('mem_status'),
            "real_name" => I('real_name'),
        );

        $db=M('member')->add($data);
        if($db) {
            $this->success('发送请求成功！',U('index'));
        }else{
            $this->error('发送请求失败');
        }
    }
    //锁定会员处理
    public function on_off_lock()
    {
        if (I('status')) {
            if (M('member')->where(array('id' => array('eq', $_GET['rid'])))->save(array('lock' => 0))) {
                $this->success('锁定成功', U('index'));
            } else {
                $this->error('锁定失败');
            }
        } /*eger quluplanghan bolsa yeshidu*/
        else {
            if (M('member')->where(array('id' => array('eq', $_GET['rid'])))->save(array('lock' => 1))) {
                $this->success('解锁成功', U('index'));
            } else {
                $this->error('解锁失败');
            }
        }
    }
    //删除会员
    public function deletemember()
    {
        if (M('member')->where(array('id' => array('eq', $_GET['rid'])))->delete()) {
            $this->success('删除成功', U('index'));
        } else {
            $this->error('删除失败');
        }
    }
    //编辑会员信息试图
    public function editmember()
    {
        $data=M('member')->where(array('id' => array('eq', I('rid'))))->select();
        $this->data=$data;
        $this->display();
    }
    //编辑会员密码试图
    public function editpwd()
    {
        $data=M('member')->where(array('id' => array('eq', I('rid'))))->select();
        $this->data=$data;
        $this->display();
    }
    //编辑密码与会员信息处理
    public function run_editmember()
    {
        if(isset($_POST['mem_password'])){
            if (I('password') != I('mem_password')) {
                $this->error('两次密码不配');
            }

            $data=array(
                "mem_name" => I('mem_name'),
                "mem_password" => I('mem_password','','md5'),

            );
        }else{
            $data=array(
                 "mem_name" => I('mem_name'),
                 "mem_phone" => I('mem_phone'),
                 "mem_email" => I('mem_email'),
                 "mem_number" => I('mem_number'),
                 "real_name" => I('real_name'),
            );

        }


        if (M('member')->where(array('id' => array('eq', I('id'))))->save($data)) {
            $this->success('修改成功', U('index'));
        } else {
           $this->error('修改失败');

        }
    }
    //审核会员
    public function on_status()
    {
        if (M('member')->where(array('id' => array('eq', $_GET['rid'])))->save(array('mem_status' => 1))) {
            $this->success('审核成功', U('index'));
        } else {
            $this->error('审核失败');
        }
    }
}